<template>
  <view class="baseForm">
    <!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
	<!-- 条码录入 -->
	
	 <u-line></u-line>
	 <div style="display: flex; align-items: center;padding: 0 10px;">
		<view class="body">
		    物料&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {{ details.materialCode }} <br>
		    数量&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {{ details.totalQuantity }}<br>
			单位&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ details.quantityUnit }}<br>
			批次&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ details.supplyBatch }}<br>
			到货日期&nbsp;&nbsp;&nbsp;&nbsp; {{details.createTime}}
		</view>
	 </div>
	 <u-line></u-line>
	
	 
  
    <!-- 提交按钮 -->
	<button type="primary" @click="leftButtonClick" class="submit_btn" :loading="posting">确定</button>
    <!-- 标准picker -->
    <u-picker :show="showStorageType" :columns="storageTypes" keyName="storageTypeName" immediateChange
              @cancel="showStorageType = false" @confirm="storageTypeSelect"></u-picker>
    <u-picker :show="showStorageSection" :columns="storageSections" keyName="storageSectionCode" immediateChange
              @cancel="showStorageSection = false" @confirm="storageSectionSelect"></u-picker>
    <u-picker :show="showBin" :columns="bins" keyName="binCode" immediateChange
              @cancel="showBin = false" @confirm="binSelect"></u-picker>
  </view>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue';
import {PageResult} from "../../../types/type";
import { onShow, onPullDownRefresh, onPageScroll } from '@dcloudio/uni-app';
import ScanBar from '@/components/scan-bar/index.vue';
import {http} from "../../../utils/http";
import ULine from "../../../uni_modules/uview-plus/components/u-line/u-line.vue";
import { CommonValue, GoodsNavClick } from '@/types/type';
// 表单数据定义
const umodel = reactive({
  storageUnitCode: '',
  storageTypeId: '',
  storageTypeCode: '',
  storageTypeName: '',
  storageSectionId: '',
  storageSectionCode: '',
  binId: '',
  binCode: '',
  deliveryCode: '',
  partnerCode: '',
  partnerName: '',
   details: []
});

// 重置表单信息 & 光标自动blur
const scanBarRef = ref();
const datekey = ref(new Date());
const props = defineProps({
  id: {
    type: String,
    default: '',
  },
  requestId: {
    type: String,
    default: '',
  },
  workshopName: {
    type: String,
    default: '',
  }
});
const details = JSON.parse(props.id);
const requestId = props.requestId;
const workshopName = props.workshopName;
onShow(()=>{
	
	console.log(details.materialName);
	console.log("requestId",requestId);
	console.log("workshopName",workshopName);
})
// 表单规则定义
const rules = ref({
  storageUnitCode: [
    {
      type: 'string',
      required: true,
      message: '请输入包装条码',
      trigger: ['blur', 'change']
    }
  ],
  storageTypeName: {
    type: 'string',
    required: true,
    message: '请选择目标存储类型',
    trigger: ['blur', 'change']
  },
  storageSectionCode: {
    type: 'string',
    required: true,
    message: '请选择存储区',
    trigger: ['blur', 'change']
  },
  partnerName: [
    {
      type: 'string',
      required: true,
      message: '请输入供应商名称',
      trigger: ['blur', 'change']
    }
  ],
  binCode: [
    {
      type: 'string',
      required: true,
      message: '请选择目标货位',
      trigger: ['blur', 'change']
    }
  ],
  
  deliveryCode: [
    {
      type: 'string',
      required: true,
      message: '送货单编号不能为空',
      trigger: ['blur', 'change']
    }
  ],
  partnerCode: [
    {
      type: 'string',
      required: true,
      message: '供应商编码不能为空',
      trigger: ['blur', 'change']
    }
  ],
});


let storageTypes = reactive([[]]);
let storageSections = reactive([[]]);
let bins = reactive([[]]);

const showStorageType = ref(false);
const showStorageSection = ref(false);
const showBin = ref(false);

async function getStorageUnitByCode(result: any) {
  umodel.storageUnitCode = result;
  const data = await http({
    url: '/wms/rmreceipt/getdelivery/' + umodel.storageUnitCode,
    method: 'GET',
    data: {}
  });
  var info = {
	warehouseId: data.data.warehouseId,
    deliveryCode: data.data.deliveryCode,
    partnerCode: data.data.partnerCode,
    partnerName: data.data.partnerName
  }
  Object.assign(umodel, info);
  umodel.details = data.data.details;
  console.log(umodel.details);


}

async function getStorageTypes() {
  const data = await http({
    url: '/wms/storagetype/getList',
    method: 'GET',
    data: {
		warehouseId: umodel.warehouseId
	}
  });
  storageTypes[0] = data.data;
}

async function getStorageSections() {
  const data = await http({
    url: '/wms/wmsstoragesection/getList',
    method: 'GET',
    data: {
      storageTypeId: umodel.storageTypeId
    }
  });
  storageSections[0] = data.data;
}

async function getBins() {
  const data = await http({
    url: '/wms/bin/getList',
    method: 'GET',
    data: {
      storageTypeId: umodel.storageTypeId,
      storageSectionId: umodel.storageSectionId
    }
  });
  bins[0] = data.data;
}

function storageTypeSelect(e) {
  if (e.value.length) {
    umodel.storageTypeId = e.value[0].id;
    umodel.storageTypeCode = e.value[0].storageTypeCode;
    umodel.storageTypeName = e.value[0].storageTypeName;
  }
  showStorageType.value = false;
  getStorageSections();
}

function storageSectionSelect(e) {
  if (e.value.length) {
    umodel.storageSectionId = e.value[0].id;
    umodel.storageSectionCode = e.value[0].storageSectionCode;
  }
  showStorageSection.value = false;
  getBins();
}

function binSelect(e) {
  if (e.value.length) {
    umodel.binId = e.value[0].id;
    umodel.binCode = e.value[0].binCode;
  }
  showBin.value = false;
}


function openStorageTypeSelect() {
  showStorageType.value = true;
  uni.hideKeyboard();
}

function openStorageSectionSelect() {
  showStorageSection.value = true;
  uni.hideKeyboard();
}

function openBinSelect() {
  showBin.value = true;
  uni.hideKeyboard();
}

// 校验提交
const uForm = ref();
const posting = ref(false);



async function leftButtonClick(item: GoodsNavClick) {
	const data = await http({
	  url: '/wms/warehousemove/materialsend',
	  method: 'POST',
	  data: {
			storageUnitCode:details.storageUnitCode,
			requestId:requestId
		}
	});
	uni.$u.toast('配送成功，物料凭证: ' + data.data.data.materialDocumentCode+',送至'+workshopName);
	
		
		uni.navigateTo({
		  url: '/pages/use/materialrequest/index',
		});
	
}
const showImport = ref(false);


</script>

<style lang="scss">
.baseForm {
	.form {
		padding: $uni-page-padding;
	}
	.submit_btn {
			margin: 20rpx;
			
		}
.upload-scan {
		padding: 10px 40rpx;
	}
  .u-form {
    margin-top: 5px;

    .scan-bar {
      display: flex;
      flex: 1;
    }
  }

  .u-line {
    padding: 10px 0;
  }

  .search {
    display: flex;

    .custom-style {
      // width: 120px;
      margin: 10px 5px 5px;
      height: 36px;
      border-radius: 8px;

      &:nth-child(1) {
        flex: 1
      }

      &:nth-child(2) {
        flex: 3
      }
    }
  }

  .list {
    .list-title {
      line-height: 40px;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.4);
    }

    .card {
      margin-top: 5px;
      margin-bottom: 10px;
      border-radius: 7px;
      display: flex;
      align-items: center;
      padding: 10px 0;
      transition: all 300ms ease;
      box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

      .left {
        // flex-basis: 90rpx;
        padding-right: 10px;
        text-align: right;
        font-size: 13px;

        .enter {
          line-height: 20px;
          margin-bottom: 5px;
        }

        .number {
          font-size: 13px;
          line-height: 18px;
        }

        :deep(.u-icon--right) {
          flex-direction: row-reverse;
        }
      }

      .right {
        flex: 1;
        padding-left: 15px;

        .title {
          font-size: 15px;
          line-height: 20px;
          margin-bottom: 5px;
        }

        .body {
          color: rgba(0, 0, 0, 0.4);
          font-size: 13px;
          line-height: 18px;
        }
      }
    }
  }
}

</style>
